﻿@model  Model.PageRole
@{
    ViewBag.Title = "任务信息列表";
    Layout = "~/Views/Shared/_Main.cshtml";
}
@section Head{
    <script type="text/javascript">
        var UrlArray = $.getUrlArray();
        var doActionOptions = "<select onchange='DoAction(this.options.selectedIndex,this.value)' style='width: 100px; '>"
        + "<option value=''>-请选择-</option>";
        var pageRole = '@Model.role';
        var opts = {
            rows: 15,
            height: 450,
            url: "/JobTask/GetTaskList",
            para: { dbName: encodeURI(UrlArray["dbName"]) },
            idField: "TaskID",
            checkbox: false,
            columns: [
                     { field: 'TaskName', title: '任务名称', width: 22, sortable: true },
                    { field: 'TaskType', title: '任务类型', width: 10, align: 'center', sortable: true,
                        formatter: function (value, rowData, rowIndex) {
                            var format = "";
                            if (value == 0) {
                                format = "重复执行";
                            }
                            else if (value == 1) {
                                format = "执行1次后删除";
                            }
                            return format;
                        }
                    },
                    { field: 'TaskIntervalDays', title: '执行周期', width: 8, align: 'center', sortable: true,
                        formatter: function (value, rowData, rowIndex) {
                            var dt = "天";
                            switch (rowData.TaskPlanType) {
                                case 2:
                                    dt = "周";
                                    break;
                                case 3:
                                    dt = "月";
                                    break;
                            }
                            var format = value > 0 ? "每隔" + value + dt : "";
                            return format;
                        }
                    },
                    { field: 'TaskStartTime', title: '执行时间', width: 20 },
                    { field: 'TaskStartDate', title: '生效日期', width: 8, align: 'center', sortable: true },
                    { field: 'TaskEndDate', title: '失效日期', width: 8, align: 'center', sortable: true },
                    { field: 'TaskStatus', title: '任务状态', width: 6, align: 'center', sortable: true,
                        formatter: function (value, rowData, rowIndex) {
                            var format = "";
                            if (value == 0) {
                                format = "<span class='grid_r'>禁用</span>";
                            }
                            else {
                                format = "启用";
                            }
                            return format;
                        }
                    },
                    { field: 'CreateTime', title: '创建时间', width: 8, align: 'center', sortable: true },
                    { field: 'TaskID', title: '操作', width: 10, align: 'center', sortable: false,
                        formatter: function (value, rowData, rowIndex) {
                            var iscopy = 0;
                            if (rowData.IsCopy == 1)
                                iscopy = 1;
                            var doActionOptions = "<select onchange='return DoAction(this.options.selectedIndex,\"" + value + "\"," + iscopy + ",\"" + rowData.TaskName + "\")' style='width: 90px; '><option value=''>-请选择-</option>";
                            if (pageRole == '2') {
                                if (rowData.IsCopy != 1) {
                                    doActionOptions += "<option>查看记录</option><option>立即执行</option><option>修改</option><option>删除</option>"
                                    if (rowData.TaskStatus == 1) {
                                        doActionOptions += "<option>禁用</option>"
                                    }
                                    else {
                                        doActionOptions += "<option>启用</option>"
                                    }
                                    return doActionOptions + "</select>";
                                } else
                                    return doActionOptions + "<option>查看记录</option><option>删除</option></select>";
                            }
                            else {
                                return doActionOptions + "<option>查看记录</option></select>";
                            }

                        }
                    }
                    ]

        };

        $(function () {
            InitList();
        });
        function InitList(postUrl) {
            if (typeof (postUrl) == "undefined" || postUrl.length == 0) {
                postUrl = "/JobTask/GetTaskList";
            }
            opts.url = postUrl;
            $("#DataGrid").dg(opts);
        }
        //“操作”下拉框事件
        function DoAction(i, value, isCopy, taskName) {
            if (isCopy == 1 && i == 2)
                i = 4;
            switch (i) {
                case 1:
                    openViewTaskLog(value, taskName); // 查看详细
                    break;
                case 2:
                    doTask(value); // 立即执行任务
                    break;
                case 3:
                    EditTask(value); // 编辑
                    break;
                case 4:
                    DeleteTask(value); // 删除
                    break;
                case 5:
                    updateTaskStatus(value); // 启用/禁用
                    break;
            }
        }

        function btnAdd_Click() {
            AddTask();
        }
        function btnView_Click() {
            openViewTaskStatus();
        }

        function btnSearch_Click() {
            var taskName = $.trim($("#txt_TaskName").val());
            var taskType = $("#ddl_TaskType").val();
            var taskStatus = $("#ddl_TaskStatus").val();
            var postUrl = "/JobTask/GetTaskList/?taskName=" + encodeURI(taskName)
                + "&taskType=" + encodeURI(taskType)
                + "&taskStatus=" + encodeURI(taskStatus)
                + "&dbName=" + encodeURI(UrlArray["dbName"]);

            thisPageNum = 1;
            InitList(postUrl);
        }

        function AddTask() {
            var DBType = "";
            if (UrlArray["DBType"] == 2)
                DBType = "&DBType=2";
            window.location.href = "/JobTask/Edit?dbName=" + encodeURI(UrlArray["dbName"]) + DBType;
        }

        function EditTask(taskID) {
            var DBType = "";
            if (UrlArray["DBType"] == 2)
                DBType = "&DBType=2";
            window.location.href = "/JobTask/Edit?taskID=" + taskID + "&dbName=" + encodeURI(UrlArray["dbName"]) + DBType;
        }
        function DeleteTask(taskID) {
            if (confirm("确定要删除吗?")) {
                $.post("/JobTask/DeleteTask?taskID=" + taskID + "&dbName=" + encodeURI(UrlArray["dbName"]), function (result) {
                    if (result.Success == true) {
                        alert(result.Message);
                    }
                    else {
                        alert(result.Message);
                    }
                    InitList();
                });
            } else
                InitList();
        }

        function doTask(taskID) {
            if (confirm("确定要立即执行吗?")) {
                var postUrl = "/JobTask/ExecuteTask?dbName=" + encodeURI(UrlArray["dbName"]);
                var postData = { taskID: taskID };
                $.ajax({
                    type: "POST",
                    async: true,
                    url: postUrl,
                    data: postData,
                    success: function (result) {
                        if (result.Success == true) {
                            alert(result.Message);
                        }
                        else {
                            alert(result.Message);
                        }
                        InitList();
                    }
                });
            } else
                InitList();
        }

        function updateTaskStatus(taskID) {
            var postUrl = "/JobTask/UpdateTaskStatus?dbName=" + encodeURI(UrlArray["dbName"]);
            var postData = { taskID: taskID };
            $.ajax({
                type: "POST",
                async: true,
                url: postUrl,
                data: postData,
                success: function (result) {
                    if (result.Success == true) {
                        alert(result.Message);
                    }
                    else {
                        alert(result.Message);
                    }
                    InitList();
                }
            });
        }

        function openViewTaskStatus() {
            var url = "/JobTask/TaskStatus?dbName=" + encodeURI(UrlArray["dbName"]);
            //window.location.href = url;
            showModalMany('TaskStatus', url, '900', '475');
            InitList();
        }
        function openViewTaskLog(taskID, taskName) {
            var url = "/JobTask/TaskLog?taskID=" + taskID + "&taskName=" + taskName + "&dbName=" + encodeURI(UrlArray["dbName"]) + "&DBType=" + UrlArray["DBType"];
            showModalMany('TaskStatus', url, '900', '500');
            InitList();
        } 
    </script>
}
<div style="margin: 5px;">
    <table class="listTitle">
        <tr>
            <td width="15">
                <img src="/images/search.png" />
            </td>
            <td width="300">
                作业任务搜索
            </td>
            <td align="right">
            </td>
        </tr>
    </table>
    <table class="searchArea" width="100%">
        <tr>
            <td>
                任务名称：
                <input id="txt_TaskName" type="text" style="width: 150px;" />
                任务类型：
                <select id="ddl_TaskType">
                    <option value="">请选择</option>
                    <option value="0">重复执行</option>
                    <option value="1">执行1次后删除</option>
                </select>
                任务状态：
                <select id="ddl_TaskStatus">
                    <option value="">请选择</option>
                    <option value="0">禁用</option>
                    <option value="1">启用</option>
                </select>
                <input id="btnSearch" type="button" value=" 搜索 " onclick="btnSearch_Click(1)" />
            </td>
        </tr>
    </table>
</div>
<div style="margin: 5px; min-height: 300px;">
    @{
        // 渲染输出列表
        var dataDictGiftCard = new ViewDataDictionary();
        dataDictGiftCard.Add("PanelTitle", "作业任务列表");
        dataDictGiftCard.Add("GridID", "DataGrid");
        if (Model.role == 2)
        {
            dataDictGiftCard.Add("ShowAddButton", true);
        }
        dataDictGiftCard.Add("ShowOtherButton", true);
        dataDictGiftCard.Add("OtherButtonID", "btnView");
        dataDictGiftCard.Add("OtherButtonValue", "任务计划表");
        dataDictGiftCard.Add("OtherButtonClick", "btnView_Click()");
        dataDictGiftCard.Add("ShowPager", true);
        dataDictGiftCard.Add("ShowLoading", true);
        @Html.Partial("_GridPanel", dataDictGiftCard);
    }
</div>
